local choufeng_active = fk.CreateSkill{
  name = "hanqing__choufeng&",
}

Fk:loadTranslationTable{
  ["hanqing__choufeng&"] = "酬烽",
  [":hanqing__choufeng&"] = "你可以使用赵胜明置的基本牌，其选择一项：1.明置一张同色牌；2.摸两张牌并令此牌无效。",

  ["#hanqing__choufeng&"] = "酬烽：你可以使用赵胜明置的基本牌",
  ["#hanqing__choufeng-choice"] = "酬烽：%dest 想使用你的%arg，你可以明置一张同色牌以允许其使用，或点“取消”摸两张牌令之无效",
}

local DIY = require "packages/diy_utility/_base"

choufeng_active:addEffect("viewas", {
  mute = true,
  pattern = ".|.|.|.|.|basic",
  prompt = "#hanqing__choufeng&",
  expand_pile = function (self, player)
    local cards = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:hasSkill("hanqing__choufeng") and p ~= player then
        table.insertTable(cards, DIY.getShownCards(p))
      end
    end
    return cards
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 and Fk:currentRoom():getCardOwner(to_select) ~= player then
      local card = Fk:getCardById(to_select)
      if card.type == Card.TypeBasic then
        return (Fk.currentResponsePattern == nil and player:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    return Fk:getCardById(cards[1])
  end,
  before_use = function (self, player, use)
    local room = player.room
    local src = room:getCardOwner(use.card.id) ---@type ServerPlayer
    if src == nil then return "" end
    src:broadcastSkillInvoke("hanqing__choufeng")
    room:notifySkillInvoked(src, "hanqing__choufeng", "support")
    room:showCards(Card:getIdList(use.card))
    room:playCardEmotionAndSound(player, use.card)
    if #use.tos > 0 and not use.noIndicate then
      room:doIndicate(player, use.tos)
    end

    local cards = table.filter(src:getCardIds("h"), function (id)
      return not table.contains(DIY.getShownCards(src), id) and
      Fk:getCardById(id):compareColorWith(use.card, false)
    end)
    local card = room:askToCards(src, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = "hanqing__choufeng",
      pattern = tostring(Exppattern{ id = cards }),
      prompt = "#hanqing__choufeng-choice::"..player.id..":"..use.card:toLogString(),
      cancelable = true,
    })
    if #card > 0 then
      DIY.showCards(src, card)
    else
      src:drawCards(2, "hanqing__choufeng")
      use.extra_data = use.extra_data or {}
      use.extra_data.hanqing__choufeng = true
    end
  end,
  enabled_at_play = function (self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and p:hasSkill("hanqing__choufeng") and
      table.find(DIY.getShownCards(p), function (id)
        return Fk:getCardById(id).type == Card.TypeBasic
      end)
    end)
  end,
  enabled_at_response = function (self, player, response)
    return not response and table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and p:hasSkill("hanqing__choufeng") and
      table.find(DIY.getShownCards(p), function (id)
        return Fk:getCardById(id).type == Card.TypeBasic
      end)
    end)
  end,
})

choufeng_active:addEffect(fk.PreCardEffect, {
  can_refresh = function (self, event, target, player, data)
    return data.from == player and data.extra_data and data.extra_data.hanqing__choufeng
  end,
  on_refresh = function (self, event, target, player, data)
    data.nullified = true
  end,
})

choufeng_active:addAI(nil, "vs_skill")

return choufeng_active
